/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This source code is subject to terms and conditions of the Shared Source License
* for DSL Editor PowerToy. A copy of the license can be found in the License.htm file
* at the root of this distribution. If you can not locate the Shared Source License
* for DSL Editor PowerToy, please obtain a copy from: http://www.codeplex.com/dsltreegrideditor/Project/License.aspx.
* By using this source code in any fashion, you are agreeing to be bound by
* the terms of the Shared Source License for DSL Editor PowerToy.
*
* You must not remove this notice, or any other, from this software.
*
* **********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DslEditorPowerToy.Controls.VirtualTreeGrid
{
    /// <summary>
    /// Represents the options that define what action to take when a keyboard event is raised
    /// </summary>
    internal enum ProcessKeyReturn
    {
        /// <summary>
        /// Branch indicated it did not want to handle the key.
        /// </summary>
        NotHandled = 0,
        /// <summary>
        /// Branch indicated it wanted the key, but no action occurred as a branchProcessingResult of handling.
        /// </summary>
        KeyHandledNoAction = 1,
        /// <summary>
        /// Branch indicated it wanted the key, and an action occurred as a branchProcessingResult of handling.
        /// </summary>
        KeyHandledActionOccurred = 2
    }
    /// <summary>
    /// Action to take for handling a key
    /// </summary>
    public enum KeyAction
    {
        /// <summary>
        /// Handle the action
        /// </summary>
        Handle,
        /// <summary>
        /// Discard the action
        /// </summary>
        Discard,
        /// <summary>
        /// Process the action
        /// </summary>
        Process
    }
    /// <summary>
    /// Keyboard naviagtion direction to take
    /// </summary>
    public enum NavigationDirection
    {
        Up,
        Down,
        Left,
        Right
    }

    /// <summary>
    /// Flags for determining actions
    /// </summary>
    [Flags]
    internal enum ActionFlag
    {
        ExpandBranch = 2,
        LocalNavigation = 4,
        StartEditMode = 8
    }

    /// <summary>
    /// Structure filled out by a navigationBranch when a key is pressed.  
    /// </summary>
    public struct ProcessKeyResult
    {
        private NavigationDirection _navigationDirection;
        private KeyAction _action ;
        private Type _branchType;
        private ActionFlag _actionFlags;

        #region Constructors
        public ProcessKeyResult(KeyAction action)
        {
            this._navigationDirection = NavigationDirection.Right;
            this._action = action;
            this._branchType = null;
            this._actionFlags = ActionFlag.LocalNavigation;
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets/sets the resulting action that should be taken in response to a key press.
        /// </summary>
        public KeyAction Action
        {
            get
            {
                return this._action;
            }
            set
            {
                this._action = value;
            }
        }

        /// <summary>
        /// Gets/sets the type of navigationBranch to set focus to  
        /// </summary>
        /// <remarks>
        /// If this is non-null, it will be processed first, then ColumnType
        /// </remarks>
        public Type BranchType
        {
            get
            {
                return this._branchType;
            }
            set
            {
                this._branchType = value;
            }
        }

        /// <summary>
        /// Gets/sets the diretion of navigation
        /// </summary>
        public NavigationDirection Direction
        {
            get
            {
                return this._navigationDirection;
            }
            set
            {
                this._navigationDirection = value;
            }
        }

        /// <summary>
        /// Gets/sets whether the current navigationBranch should be expanded before further navigation
        /// </summary>
        public bool ExpandBranch
        {
            get
            {
                return GetActionFlag(ActionFlag.ExpandBranch);
            }
            set
            {
                SetActionFlag(ActionFlag.ExpandBranch, value);
            }
        }

        /// <summary>
        /// Gets/sets whether the navigation should stop at the current navigationBranch or splill into next/previous navigationBranch
        /// </summary>
        public bool LocalBranchNavigationOnly
        {
            get
            {
                return GetActionFlag(ActionFlag.LocalNavigation);
            }
            set
            {
                SetActionFlag(ActionFlag.LocalNavigation, value);
            }
        }

        /// <summary>
        /// Gets/sets whether the cell should enter edit mode
        /// </summary>
        public bool StartLabelEdit
        {
            get
            {
                return GetActionFlag(ActionFlag.StartEditMode);
            }
            set
            {
                SetActionFlag(ActionFlag.StartEditMode, value);
            }
        }

        #endregion

        #region Private Methods
        private bool GetActionFlag(ActionFlag flagValue)
        {
            return ((this._actionFlags & flagValue) != 0);
        }
        private void SetActionFlag(ActionFlag flagValue, bool value)
        {
            if (value)
                this._actionFlags |= flagValue;
            else
                this._actionFlags &= ~flagValue;
        }
        #endregion
    }
}
